package com.kingdee.pda.ui.popupwindow;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.kingdee.pda.R;
import com.kingdee.pda.adapter.AbsBaseAdapter;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.util.WarehouseUtil;
import com.kingdee.pda.callback.OnConfirmClickCallback;
import com.kingdee.pda.ui.widget.SelectorTextView;
import com.kingdee.pda.util.KeyboardUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhanlg
 * DATE 2021/5/21
 * TIME 20:15
 */
public class WarehouseFilterPpWindow extends PopupWindow implements View.OnClickListener {

    private Context mContext;
    private int mWindowWidth;
    private List<Warehouse> mData;
    private OnConfirmClickCallback<Warehouse> mCallback;
    private EditText mEtSearch;
    private ListView mListView;
    private WarehouseListAdapter mAdapter;
    private List<Warehouse> mShowList;
    private Warehouse mCurrentWarehouse;


    public WarehouseFilterPpWindow(Context context, int width, List<Warehouse> data, OnConfirmClickCallback<Warehouse> callback) {
        super(context);
        this.mContext = context;
        this.mWindowWidth = width;
        this.mData = data;
        this.mCallback = callback;

        initLayout();
    }

    private void initLayout() {
        setWidth(mWindowWidth);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_warehouse_filter, null);
        setBackgroundDrawable(mContext.getResources().getDrawable(R.color.color_lucency));
        setContentView(contentView);
        setOutsideTouchable(true);

        initView(contentView);
        initData();
    }

    private void initView(View contentView) {
        mCurrentWarehouse = WarehouseUtil.getCurrentWarehouse();
        if (mCurrentWarehouse == null && mData != null && mData.size() > 0) {
            mCurrentWarehouse = mData.get(0);
        }
        mEtSearch = contentView.findViewById(R.id.et_search);
        mListView = contentView.findViewById(R.id.list_view);
        contentView.findViewById(R.id.iv_clear_input).setOnClickListener(this);
        contentView.findViewById(R.id.view_bottom).setOnClickListener(this);

        setListViewClickListener();
        addTextChangeListener();
    }

    private void setListViewClickListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Warehouse warehouse = mAdapter.getItem(position);
                mCallback.onConfirmClick(warehouse);
                dismiss();
            }
        });
    }

    private void addTextChangeListener() {
        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String input = mEtSearch.getText().toString();
                if (TextUtils.isEmpty(input)) {
                    mAdapter.setData(mData);
                } else {
                    filteData(input);
                }
            }
        });
    }

    private void initData() {
        mAdapter = new WarehouseListAdapter(mContext);
        mListView.setAdapter(mAdapter);
        mAdapter.setData(mData);
    }

    private void filteData(String input) {
        if (mShowList == null) {
            mShowList = new ArrayList<>();
        } else {
            mShowList.clear();
        }
        for (Warehouse warehouse : mData) {
            if (warehouse.getName().startsWith(input)) {
                mShowList.add(warehouse);
            }
        }
        for (Warehouse warehouse : mData) {
            if (!warehouse.getName().startsWith(input) && warehouse.getName().contains(input)) {
                mShowList.add(warehouse);
            }
        }
        mAdapter.setData(mShowList);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_clear_input:
                mEtSearch.setText("");
                break;
            case R.id.view_bottom:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        KeyboardUtils.closeSoftKeybord(mEtSearch, mContext);
    }

    class WarehouseListAdapter extends AbsBaseAdapter<Warehouse> {

        public WarehouseListAdapter(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_common_list_select, parent, false);
            }
            SelectorTextView tvName = convertView.findViewById(R.id.tv_name);
            ImageView ivSelected = convertView.findViewById(R.id.iv_selected);

            final Warehouse wareHouse = getData().get(position);
            tvName.setText(wareHouse.getName());
            if (mCurrentWarehouse == null) {
                ivSelected.setVisibility(View.INVISIBLE);
            } else {
                ivSelected.setVisibility(mCurrentWarehouse.getId() == wareHouse.getId() ? View.VISIBLE : View.INVISIBLE);
            }

            return convertView;
        }
    }
}
